package com.rd.student.dashuju.zhengchengxiang.Day0731;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;


public class TestRandom01 {
    public static void main(String[] args) {
        // 随机出50以内（不包括50），不重复的20个数字
        random01();
//        random02();
//        random03();
//        random04();
//        random05();

    }

    public static void random05() {
        int[] arr = new int[50];
        LinkedList list = new LinkedList();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = i + 1;
        }
        for (int i = 0; i < 20; i++) {
            int index = (int) (Math.random() * 50);
            if (arr[index] != -1) {
                list.add(arr[index]);
            }
            arr[index] = -1;
        }
        System.out.println(list);
    }

    public  static void random04() {
        int[] arr = new int[50];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=i+1;
        }
        HashSet list = new HashSet();
        while (list.size()<=20){
            int random = (int) (Math.random() * 50);
            list.add(random);
        }
        System.out.println(list);
    }

    public static void random03() {
        int[] arr = new int[50];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=i+1;
        }
        ArrayList<Integer> list = new ArrayList<>();
        while (list.size()<=20){
            int random = (int) (Math.random() * 50);
            if(list.contains(random)){
                continue;
            }
            list.add(random);
        }
        System.out.println(list);
    }

    public static void random02() {
        int[] arr = new int[50];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = -1;
        }

        for (int i = 0; i < 20; i++) {
            int random = (int) (Math.random() * 50);
            if (arr[random] == -1) {
                arr[random] = random;
                System.out.println(arr[random]);
            } else {
                i--;
            }
        }
    }

    public static void random01() {
        int[] arr = new int[20];
        for (int i = 0; i < 20; i++) {
            int random = (int) (Math.random() * 50);
            boolean flag = false;
            // 判断是否存在
            for (int i1 = 0; i1 < i; i1++) {
                if (random == arr[i1]) {
                    flag = true;
                    break;
                }
            }
            // 存在就重新随机
            if (flag) {
                i--;
                continue;
            }
            arr[i] = random;
            System.out.println(arr[i]);
        }
    }
}